import csv
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
s = Service("chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.implicitly_wait(10)
driver.get("https://tw.hotels.com")
#XPath定位出前往地點欄位
location = driver.find_element(By.XPATH, "//button[@class='uitk-fake-input uitk-form-field-trigger']")
location.click()
city = driver.find_element(By.XPATH, "//input[@id='destination_form_field']")
city.send_keys("台北")
city.send_keys(Keys.ENTER);
time.sleep(3)
#XPath定位出搜尋按鈕
search = driver.find_element(By.XPATH, "//button[@id='submit_button']")
search.click()
time.sleep(3)
#XPath定位出顯示更多按鈕
more = driver.find_element(By.XPATH, "//button[@class='uitk-button uitk-button-medium uitk-
button-secondary']")
more.click()
time.sleep(3)
#XPath定位飯店相關資料欄位
hotels = driver.find_elements(By.XPATH, "//div[@class='uitk-card uitk-card-roundcorner-all uitk-card-has-primary-theme']")
# 開啟CSV檔案寫入截取的資料
with open("hotel.csv", 'w+', newline='', encoding="utf-8") as fp:
writer = csv.writer(fp)
for hotel in hotels:
#HTML標籤定位飯店名稱、地區、價錢、評價資訊
name = hotel.find_element(By.CLASS_NAME, "uitk-heading-5")
place = hotel.find_element(By.CLASS_NAME, "uitk-text")
price = hotel.find_element(By.CLASS_NAME, "uitk-type-600")
star = hotel.find_element(By.CLASS_NAME, "uitk-spacing-padding-inlineend-one")
rowList = [name.text, place.text, price.text, star.text]
writer.writerow(rowList)
#印出擷取資料
print("飯店: " + name.text)
print("區域: " + place.text)
print("價錢: "+ price.text)
print("評價: "+ star.text)
print('————————————————————————————————————————————')
driver.quit()